package com.example.yuejian.yuejian.model;

import com.example.yuejian.yuejian.base.ModleListListener;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.CommiteQuestion;
import com.example.yuejian.yuejian.bean.MasterClass;
import com.example.yuejian.yuejian.bean.MasterInfo;
import com.example.yuejian.yuejian.bean.Masters;
import com.example.yuejian.yuejian.bean.Question;
import com.example.yuejian.yuejian.service.MastersService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/9/18.
 */

public class MastersModel {
    private Retrofit retrofit;

    public MastersModel() {
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Contants.BASE_URL)
                .build();

    }

    //列表
    public  void   getMasterList(final ModleListListener listener) {
        MastersService service = retrofit.create(MastersService.class);
        Call<List<Masters>> call = service.Masterlist();
        call.enqueue(new Callback<List<Masters>>() {
            @Override
            public void onResponse(Call<List<Masters>> call, Response<List<Masters>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<List<Masters>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }


    //大师信息
    public void masterInfo(int masterid, final ModleListener listener){
        MastersService service=retrofit.create(MastersService.class);
        Call <MasterInfo>call=service.Master_info( masterid);
        call.enqueue(new Callback<MasterInfo>() {
            @Override
            public void onResponse(Call<MasterInfo> call, Response<MasterInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");


            }

            @Override
            public void onFailure(Call<MasterInfo> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });


    }
    //大师课程列表
    public void MasterClassList(String mid,final ModleListListener listListener){
        MastersService service=retrofit.create(MastersService.class);
        Call<List<MasterClass>>call=service.MasterclassList(mid);
        call.enqueue(new Callback<List<MasterClass>>() {
            @Override
            public void onResponse(Call<List<MasterClass> >call, Response<List<MasterClass>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else
                    listListener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<List<MasterClass>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //大师课程列表
    public void QusetionList(String mid, final ModleListListener listListener){
        MastersService service=retrofit.create(MastersService.class);
        Call<List<Question>>call=service.questionList(mid);
        call.enqueue(new Callback<List<Question>>() {
            @Override
            public void onResponse(Call<List<Question>> call, Response<List<Question>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else
                    listListener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<List<Question>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //提问大师
    public void AskMaster(String userid,String content,String Masterid,String usename,final ModleListener listener)
    {
        MastersService service=retrofit.create(MastersService.class);
        Call<CommiteQuestion>call=service.commitequestion(userid,content,Masterid,usename);
        call.enqueue(new Callback<CommiteQuestion>() {
            @Override
            public void onResponse(Call<CommiteQuestion> call, Response<CommiteQuestion> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<CommiteQuestion> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}

